const jwt = require('jsonwebtoken');

module.exports = async (ctx, next) => {
    const token = ctx.header.authorization?.replace('Bearer ', '');
    console.log(token);
    if (!token) {
        ctx.body = {
            code: 401,
            message: 'No token',
        };
        return;
    }
    try {
        const decoded = jwt.verify(token, 'scx123');
        ctx.state.user = decoded;
        await next(); // 继续执行后续中间件
    } catch (err) {
        ctx.body = {
            code: 403,
            message: 'Invalid token',
        };
    }
};

